<template>
  <div class="article-container">
    <el-card class="box-card">
      <!-- 搜索 -->
      <el-row :gutter="20">
        <!-- 输入框查找 -->
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="input3"
            class="input-with-select"
            size="medium"
          >
            <el-select v-model="select" slot="prepend" placeholder="分类">
              <el-option label="餐厅名" value="1"></el-option>
              <el-option label="订单号" value="2"></el-option>
              <el-option label="用户电话" value="3"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
       <!-- 状态查找 -->
        <el-col :span="2">
          <el-select v-model="value" clearable placeholder="状态" size="medium" >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
         <!-- 日期查找 -->
        <el-col :span="13">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            size="medium"
            style="right: 20px;"
          >
          </el-date-picker>
           <el-button type="primary" icon="el-icon-search" size="small"
            >查询</el-button
          >
          <el-button type="info" icon="el-icon-refresh-left" size="small"
            >重置</el-button
          >
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table
        :data="articleList"
        style="width: 100%"
        stripe
        lazy
        @selection-change="handleSelectionChange"
        >>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="封面" width="180" align="center">
          <template v-slot="slot">
            <el-image
              :preview-src-list="[slot.row.cover_img]"
              :src="slot.row.cover_img"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="aut_name" label="作者" width="80" align="center">
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="发布时间"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="stutas" label="状态" width="80" align="center">
          <template v-slot="slot">
            <el-tag type="success" v-if="slot.row.stutas">已发布</el-tag>
            <el-tag v-else>草稿箱</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="channel_id"
          label="分类"
          width="80"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="address" label="操作" width="180" align="center">
          <template v-slot="slot">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="editClick(slot.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="deleteClick(slot.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticleList, deleteArticle } from "@/api/article";
export default {
  name: "Article",
  data() {
    return {
      articleList: [], // 文章表格数据
      queryInfo: {
        query: "", // 查询参数
        pageNum: 1, // 分页页码
        pageSize: 10, // 页面数据条数
      },
      total: 0,
      multipleSelection: [], // 全选数据
      input3: "",
      select: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value: "",
      value2: "",
      options: [
        {
          value: "true",
          label: "已发布",
        },
        {
          value: "false",
          label: "草稿箱",
        },
      ],
    };
  },
  created() {
    this.getArticleList();
  },
  methods: {
    // 文章表格数据
    async getArticleList() {
      const { list, pageNum, pageSize, total } = await getArticleList(
        this.queryInfo
      );
      this.articleList = list;
      this.queryInfo.pageNum =Number(pageNum);
      this.queryInfo.pageSize =Number(pageSize);
      this.total = total;
    },
    // 设置页面数据条数
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.getArticleList();
    },
    // 设置分页页码
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val;
      this.getArticleList();
    },
    // 编辑用户
    editClick(row) {
      this.$message({
        message: "该功能未实现",
        type: "warning",
      });
    },
    // 删除用户
    deleteClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await deleteArticle(row.id);
          this.getArticleList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 全选数据
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.map((item) => {
        this.multipleSelection.push(item.id);
      });
    },
  },
};
</script>

<style lang="scss">
.el-select .el-input {
  width: 80px;
}
.box-card {
  margin: 20px;
}
.el-table {
  margin: 20px 0px;
}
.el-image {
  border-radius: 5px;
}
</style>
